{% extends "base.html" %}
{% load staticfiles %}

{% block page_content %}
<section class="content-header" style="padding-top:0px" xmlns="http://www.w3.org/1999/html">
<div class="row">
    <div class="box box-info">
        <div class="box-header">
            <h2>Monitoring, Workers & Queue management</h2>
        </div>
    </div>
</div>
</section>
<section class="content">
    <div class="row">
        <div class="col-lg-3 col-md-6" >
            <form method="post" action="{% url "management" %}">{% csrf_token %}
            <div class="box box-primary">
                <div class="box-header with-border"><h4>Inspect</h4></div>
                <div class="box-body">
                    <div class="row">
                        <div class="col-sm-4"><button class="btn btn-primary pull-right" type="submit" name="op" value="list_workers">Workers</button></div>
                        <div class="col-sm-4"><button class="btn btn-primary pull-right" type="submit" name="op" value="list">Managers</button></div>
                        <div class="col-sm-4"><button class="btn btn-primary pull-right" type="submit" name="op" value="gpuinfo">GPU memory</button></div>
                    </div>
                </div>
            </div>
            </form>
        </div>
        <div class="col-lg-3 col-md-6">
            <div class="box box-primary">
                <form method="post" action="{% url "management" %}">{% csrf_token %}
                <div class="box-header with-border"><h4>Launch queue</h4></div>
                <div class="box-body">
                    <div class="row">
                    <div class="col-sm-4"><div class="form-group"><label for="host_name">Host name<input name="host_name" required="required" type="text" class="form-control"></label></div></div>
                    <div class="col-sm-4"><div class="form-group"><label for="queue_name">Queue name<input name="queue_name" type="text" required="required" class="form-control"></label></div></div>
                    <div class="col-sm-4"><button style="margin-top:20px" class="btn btn-primary form-actions" type="submit" name="op" value="launch">Launch queue</button></div>
                    </div>
                </div>
                </form>
            </div>
        </div>
        <div class="col-lg-6 col-md-12">
        <div class="box box-primary">
            <div class="box-header with-border">
                <h4>Management Actions</h4>
            </div>
            <div class="box-body">
                <table class="table dataTables-dict">
                <thead>
                <tr>
                    <th>op</th>
                    <th>host</th>
                    <th>message</th>
                    <th>time</th>
                </tr>
                </thead>
                <tbody>
                    {% for k in actions %}
                        <tr>
                            <td>{{ k.op }} </td>
                            <td>{{ k.host }}</td>
                            <td>{{ k.message }}</td>
                            <td>{{ k.created|timesince }}</td>
                        </tr>
                    {% endfor %}
                </tbody>
                </table>
            </div>
        </div>
        </div>
    </div>
    {% if queues %}
    <div class="row">
        <div class="col-lg-12">
        <div class="box box-primary">
            <div class="box-header with-border">
                <h4>Queue list (timeout = {{ timeout }})</h4>
            </div>
            <div class="box-body">
                <table class="table dataTables-dict">
                <thead>
                <tr>
                    <th>Worker name</th>
                    <th>queue name</th>
                    <th>routing key</th>
                </tr>
                </thead>
                <tbody>
                    {% for name,qs in queues.items %}
                        {% for q in qs %}
                        <tr>
                            <td>{{ name }} </td>
                            <td>{{ q.name }}</td>
                            <td>{{ q.routing_key }}</td>
                        </tr>
                        {% endfor %}
                    {% endfor %}
                </tbody>
                </table>
            </div>
        </div>
        </div>
    </div>
    {% endif %}
    <div class="row">
        <div class="col-lg-12">
            <div class="box box-primary">
                <div class="box-header"><h2>System state</h2></div>
                <div class="box-body">
                <table class="table dataTables">
                <thead>
                <tr>
                    <th>Processes</th>
                    <th>Tasks</th>
                    <th>Pending tasks</th>
                    <th>Completed tasks</th>
                    <th>Timestamp</th>
                    <th>Since</th>
                </tr>
                </thead>
                <tbody>
                    {% for k in state %}
                        <tr>
                            <td>{{ k.processes }} </td>
                            <td>{{ k.tasks }}</td>
                            <td>{{ k.pending_tasks }}</td>
                            <td>{{ k.completed_tasks }}</td>
                            <td>{{ k.created }}</td>
                            <td>{{ k.created|timesince }}</td>
                        </tr>
                    {% endfor %}
                </tbody>
                </table>
                </div>
            </div>
        </div>
    </div>
</section>
{% endblock %}
{% block page_js%}
<script>
    $(document).ready(function() {
        InitializeTables();
    })
</script>
{% endblock %}
